﻿/// <reference path="../Scripts/jquery-1.4.1-vsdoc.js" />
/// <reference path="../Scripts/jquery-ui-1.8.10.custom.js" />

(function ($, undefined) {

    /// <reference path="../Scripts/jquery-1.4.1-vsdoc.js" />
    $.zx = $.zx || {};
    if ($.zx.version) {
        return;
    }


})(jQuery)

$.zx.options = {
    version:"0.001"
};

$.zx.Processing = function (el,msg) {

};

jQuery.handleErrorResponse = function (xhr)
{
	var msg = "";
	var dev = 1;
	if (xhr.status == 403)
	{
		msg = "对不起，请先登录";
		setTimeout(function ()
		{
			document.location.href = "/";
		}, 1000);
	}
	if (xhr.status >= 500 && dev)
	{
		msg = xhr.responseText;
	}
	$("body").mask(msg, 1000);
};

jQuery.fn.extend({ mask: function (message, delay)
{
	var maskId = "_mask_id_";
	if ($("#" + maskId).length == 0)
	{
		$("body").append($("<div></div>").attr("id", maskId).hide());
	}
	var mask = $("#" + maskId);
	mask.html(message);
	mask.dialog({ modal: true });
	mask.dialog("open");
	if (delay)
	{
		setTimeout(function ()
		{
			mask.dialog("close");
		}, delay);
	}
},
	unmask: function ()
	{
		var maskId = "_mask_id_";
		$("#" + maskId).dialog("close");
	}
});

jQuery.fn.extend({ ajaxForm: function (option)
{
	var _options = {
		action: "",
		method: "post",
		summary: "#summary",
		validate: true,
		success: undefined,
		failed: undefined,
		dialog: undefined,
		autoClose: true,
		messages: {
			processing: "正在保存",
			success: "保存成功"
		}
	};
	$.extend(_options, option);
	return this.each(function (index, el)
	{
		var _self = $(el);
		if (_options.validate)
		{
			_self.validate();
		}
		_self.unbind("submit");
		_self.submit(function ()
		{
			var action = _self.attr("action"),
            method = _self.attr("method");
			if (!_self.valid())
				return false;

			$("body").mask(_options.messages.processing);

			$.ajax({
				url: action,
				type: method,
				data: _self.serializeArray(),
				success: function (result)
				{
					if (result.success)
					{
						$("body").mask(_options.messages.success, 1000);
						_option.success && _option.success(result);
						_option.autoClose && _option.dialog && _option.dialog.dialog("close");
					}
					else
					{
						$("body").unmask();
						_this.validate().showErrors(result.errors);
						$(_options.summary).html((result.msg || "") + $(_options.summary).html()).show();
						_option.failed && _option.failed(result);
					}
				},
				error: handleErrorResponse
			});

			return false;
		})
	});
} 
});


jQuery.fn.extend({
	ajaxDialog: function (options)
	{
		if (this.selector == "")
		{
			return $("<div></div>").appendTo("body").ajaxDialog(options);
		}
		if (options == undefined || options.url == undefined)
			return $(this);
		_this = this;
		options.modal = options.modal == undefined ? true : options.modal;
		this.load(options.url, options.data || {},
                function (responseText, textStatus, xhr)
                {
                	if (xhr.status != 200)
                	{
                		jQuery.handleErrorResponse(xhr);
                		return;
                	}
                	_this.dialog(options).beWidgets();
                }
                );
		return $(this);
	}
});

jQuery.fn.extend({
	beWidgets: function ()
	{
		return this.each(function ()
		{
			$(".def-button", this).button();
			$(".def-tabs", this).tabs({
				cache: true,
				ajaxOptions: {
					error: function (xhr, status, index, anchor)
					{
						$(anchor.hash).html("未能加载内容");
					},
					success: function (xhr, status, index, anchor)
					{
						$(anchor.hash).beWidgets();
					}
				}
			});
			$(".def-buttonset", this).buttonset();
			$(".def-ajax-form", this).ajaxForm();
			$(".def-dialog-link", this).click(function ()
			{
				var self = $(this);
				$(document).ajaxDialog({ url: self.attr("href") });
				return false;
			});

			$(".def-close").click(function ()
			{
				$(this).parents(".ui-dialog-content").dialog("close");
			})
		})
	}
});
